package com.joysuch.wwyt.core.controller;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import cn.zlg.common.util.CollectionUtils;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.entity.BaseManageType;
import com.joysuch.wwyt.core.entity.BaseManageTypeVo;
import com.joysuch.wwyt.core.service.BaseManageTypeService;
import com.joysuch.wwyt.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import retrofit2.http.POST;

import javax.validation.Valid;
import java.util.List;

/**
 * @author : pp
 * @date : Created in 2021/4/14 10:28
 */
@RestController
@RequestMapping("/base/manage/type")
@Api(tags = "部门-管理类型设置")
public class BaseManageTypeController {
    @Autowired
    private BaseManageTypeService manageTypeService;

    @RequiresPermissions("base:manage:type:add")
    @PostMapping("add")
    @ApiOperation("新增管理类型")
    public ResultBean add(@Valid @RequestBody BaseManageType entity) {
        manageTypeService.add(entity);
        return ResultBean.success("管理类型添加成功");
    }

//    @RequiresPermissions("base:manage:type:findAll")
    @GetMapping("findAll")
    @ApiOperation("查询部门下所有管理类型分页")
    public ResultBean findAll(@NotNull(message = "部门id不能为空") Long departId, @NotNull(message = "当前页不能为空") Integer pageNum,
                              @NotNull(message = "每页大小不能为空") Integer pageSize) {
        PageInfo<BaseManageTypeVo> list = manageTypeService.findAll(departId, pageNum + 1, pageSize);
        return ResultBean.pageData(list.getLists(), list.getTotalCount());
    }

    @RequiresPermissions("base:manage:type:getAll")
    @GetMapping("getAll")
    @ApiOperation("查询部门下所有管理类型")
    public ResultBean findAll(@NotNull(message = "部门id不能为空") Long departId) {
        List<BaseManageTypeVo> list = manageTypeService.getAll(departId);
        return ResultBean.success(list);
    }

    @RequiresPermissions("base:manage:type:delete")
    @PostMapping("delete")
    @ApiOperation("批量删除")
    public ResultBean delete(@RequestBody List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new IllegalArgumentException("请勾选删除项");
        }
        manageTypeService.delete(ids);
        return ResultBean.success("删除成功");
    }

}
